

package com.wake.widget.widget;

import android.content.Context;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

import androidx.core.view.WindowInsetsCompat;

import com.wake.widget.util.NMUINotchHelper;
import com.wake.widget.util.NMUIWindowInsetHelper;

public class NMUINotchConsumeLayout extends FrameLayout {
    public NMUINotchConsumeLayout(Context context) {
        this(context, null);
    }

    public NMUINotchConsumeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NMUINotchConsumeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        NMUIWindowInsetHelper.setOnApplyWindowInsetsListener(this, new androidx.core.view.OnApplyWindowInsetsListener() {
            @Override
            public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
                notifyInsetMaybeChanged();
                return insets;
            }
        }, true);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (!NMUINotchHelper.isNotchOfficialSupport()) {
            notifyInsetMaybeChanged();
        }
    }

    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (!NMUINotchHelper.isNotchOfficialSupport()) {
            notifyInsetMaybeChanged();
        }
    }

    public boolean notifyInsetMaybeChanged() {
        setPadding(
                NMUINotchHelper.getSafeInsetLeft(this),
                NMUINotchHelper.getSafeInsetTop(this),
                NMUINotchHelper.getSafeInsetRight(this),
                NMUINotchHelper.getSafeInsetBottom(this)
        );
        return true;
    }
}
